package com.dhkj.web.utils;


import android.app.Activity;
import android.content.Context;


import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.bean.Bmzl;
import com.dhkj.web.bean.Demo;
import com.dhkj.web.bean.OrderProductModel;
import com.dhkj.web.bean.PrintReportModel;
import com.dhkj.web.bean.ProductModel;
import com.dhkj.web.bean.ResultScanDinner;
import com.dhkj.web.bean.ScanDinner;
import com.dhkj.web.bean.TangReportTime;
import com.dhkj.web.bean.Zweekmenubak;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


import io.reactivex.Observer;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 提交参数方式
 */
public class RequestUtils {

    /**
     * Get 请求demo
     */
    public static void getDemo(Activity context, Observer<Demo> observer, String username, String password) {
        JSONObject root = new JSONObject();
        try {
            root.put("username", username);
            root.put("password", password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().postUser(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void subOfflinePay(Activity context, MyObserver observer, String body, String auth_code, String je,
                                     String WXdh, String sub_mch_id, String sub_appid) {
        RetrofitPayUtils.getApiUrl().subOfflinePay(body, auth_code, je, WXdh, sub_mch_id, sub_appid)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void subWXPayCheck(Activity context, WXPayCheckObserver observer, String WXdh, String sub_mch_id, String sub_appid) {
        RetrofitPayUtils.getApiUrl().subWXPayCheck(WXdh, sub_mch_id, sub_appid)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void getSelect(Activity context, MyObserver observer, String sql) {
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).split("-");
        String year = strNow[0];
        String month = strNow[1];
        String day = strNow[2];
        String userid = "G" + year + "Z" + month + "D" + day + "H";
        RetrofitUtils.getApiUrl()
                .runSelect(userid, sql).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void runSpSelect(Activity context, MyObserver observer, String sql) {
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).split("-");
        String year = strNow[0];
        String month = strNow[1];
        String day = strNow[2];
        String userid = "G" + year + "Z" + month + "D" + day + "H";
        RetrofitUtils.getApiUrl()
                .runSpSelect(userid, sql).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void runSpSelectContext(Context context, MyObserver2 observer, String sql) {
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).split("-");
        String year = strNow[0];
        String month = strNow[1];
        String day = strNow[2];
        String userid = "G" + year + "Z" + month + "D" + day + "H";
        RetrofitUtils.getApiUrl()
                .runSpSelect(userid, sql).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void test(Context context, MyObserverRes<ResultScanDinner> observer, int status, String cardno, String dinner, String pass) {
        RetrofitUtils.getApiUrl().getTest(status, cardno, dinner, pass).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void getProduct(Context context, String posnum, MyObserverRes<List<ProductModel>> observer) {
        RetrofitUtils.getApiUrl().getProduct(posnum).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void updateStatus(Context context, MyObserverRes<List<ScanDinner>> observer, String pkids, String caedno, String dinner) {
        RetrofitUtils.getApiUrl().updateStatus(pkids, dinner, caedno).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getAddr(Context context, String operaId, MyObserverRes<List<AddrTree>> observer) {
        RetrofitUtils.getApiUrl().getAddr(operaId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getBmzl(Context context, MyObserverRes<Map<Integer, List<Bmzl>>> observer) {
        RetrofitUtils.getApiUrl().getBmzl().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getZWeekMenuBak(Context context, MyObserverRes<Map<Integer, List<Zweekmenubak>>> observer) {
        RetrofitUtils.getApiUrl().getZWeekMenuBak().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void toPrePay(Context context, MyObserverRes<Map<String, String>> observer, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().toPrePayBatch(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void preFacePay(Context context, MyObserverRes<Map<String, String>> observer, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().preFacePay(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void facePay(Context context, String json, MyObserverRes<String> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .facePay(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void microPay(Context context, String order, String authCode, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().microPayBatch(order, authCode).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void checkOrder(Context context, String order, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().checkOrderBatch(order).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void checkAliOrderStatus(Context context, String order, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().checkAliOrderStatus(order).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void cashPay(Context context, MyObserverRes<String> observer, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().cashPayBatch(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }


    public static void depotCashPayBatch(Context context, MyObserverRes<String> observer, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().depotCashPayBatch(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void getFk(Context context, String cardsn, MyObserverRes<com.alibaba.fastjson.JSONObject> observer) {

        RetrofitUtils.getApiUrl().getFk(cardsn)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void toDepotPrePayBatch(Context context, MyObserverRes<com.alibaba.fastjson.JSONObject> observer, String json, String cardsn) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().toDepotPrePayBatch(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void tuikuoOrder(Context context, MyObserverRes<String> observer, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl().tuikuoOrder(requestBody)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    public static void getReportTime(Context context, String operaId, MyObserverRes<List<TangReportTime>> observer) {
        RetrofitUtils.getApiUrl().getTime(operaId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void order(Context context, String posnum, MyObserverRes<List<OrderProductModel>> observer) {
        RetrofitUtils.getApiUrl().order(posnum).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getReport(Context context, int addr, String timeId, String date, String posnum, MyObserverRes<PrintReportModel> observer) {


        RetrofitUtils.getApiUrl().getReport(addr, timeId, date, posnum).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void login(Context context, String user, String pass, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().login(user, pass).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void jiesuan(Context context, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().jiesuan().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void checkJiesuan(Context context, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().checkJiesuan().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

        /**
         * Post 请求demo
         */
//    public static void postDemo(RxAppCompatActivity context, String name, String password, Observer<Demo> consumer){
//        RetrofitUtils.getApiUrl()
//                .postUser(name,password).compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }
        /**
         * Put 请求demo
         */
//    public static void putDemo(RxFragment context, String access_token,Observer<Demo> consumer){
//        Map<String, String> headers = new HashMap<String, String>();
//        headers.put("Accept","application/json");
//        headers.put("Authorization",access_token);
//        RetrofitUtils.getApiUrl()
//                .put(headers,"厦门").compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }
        /**
         * Delete 请求demo
         */
//    public static void deleteDemo(RxFragment context, String access_token,Observer<Demo> consumer){
//        RetrofitUtils.getApiUrl()
//                .delete(access_token,1).compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }

        /**
         * 上传图片
         */
//    public static void upImagView(RxFragment context, String  access_token,String str, Observer<Demo>  observer){
//        File file = new File(str);
////        File file = new File(imgPath);
//        Map<String,String> header = new HashMap<String, String>();
//        header.put("Accept","application/json");
//        header.put("Authorization",access_token);
////        File file =new File(filePath);
//        RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
////        RequestBody requestFile =
////                RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        MultipartBody.Part body =
//                MultipartBody.Part.createFormData("file", file.getName(), reqFile);
//        RetrofitUtils.getApiUrl().uploadImage(header,body).compose(RxHelper.observableIO2Main(context))
//                .subscribe(observer);
//    }

        /**
         * 上传多张图片
         */
//    public static void upLoadImg(RxFragment context,String access_token,List<File> files, Observer<Demo>  observer1){
//        Map<String,String> header = new HashMap<String, String>();
//        header.put("Accept","application/json");
//        header.put("Authorization",access_token);
//        MultipartBody.Builder builder = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM);//表单类型
//        for (int i = 0; i < files.size(); i++) {
//            File file = files.get(i);
//            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/*"), file);
//            builder.addFormDataPart("file", file.getName(), photoRequestBody);
//        }
//        List<MultipartBody.Part> parts = builder.build().parts();
//        RetrofitUtils.getApiUrl().uploadImage1(header,parts).compose(RxHelper.observableIO2Main(context))
//                .subscribe(observer1);
//    }
}

